package views;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
 
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.plaf.PanelUI;

import org.jhotdraw.draw.Drawing;
import org.jhotdraw.draw.DrawingEvent;
import org.jhotdraw.draw.DrawingListener;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.samples.draw.DrawingPanel;
import org.joda.time.DateTime;

import drawing.DrawingComponent;
import drawing.State;

import utils.MyEventClassListener;
import utils.Options;
import utils.TaskFactory;

import net.miginfocom.swing.MigLayout;
import network.MasterCommunicator;

public class GameWindow {

	JFrame mainFrame;
	JTextArea chatArea;
	JTextField chatInput;
	JPanel mainPanel;
	JButton dCirce, dRect;
	JScrollPane chatPanel;
	DrawingComponent drawingArea;
	// DrawingPanel dpanel;
	int lastMessageId = 0;
	String username;
	MasterCommunicator comm;
	JButton btnRect, btnEllipse, btnLine, btnPen;
	

	public GameWindow(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		// mainFrame = new JFrame("Draw With Me");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setSize(new Dimension(800, 600));
		btnRect = new JButton("R");
		btnEllipse = new JButton("E");
		btnLine = new JButton("L");
		btnPen = new JButton("P");

		mainPanel = new JPanel(new MigLayout());

		addWidgets();
		addListeners();

		mainFrame.getContentPane().removeAll();
		mainFrame.getContentPane().add(mainPanel);
		mainFrame.pack();
		mainFrame.setVisible(true);

		username = Options.getInstance().getUser_name();

		comm = MasterCommunicator.getMasterCommunicator();
		comm.setGameWindowRef(this);
	}

	private void addWidgets() {

		chatArea = new JTextArea("");
		chatInput = new JTextField("");
		chatPanel = new JScrollPane(chatArea);
		drawingArea = new DrawingComponent();
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 4));
		buttonPanel.add(btnRect);
		buttonPanel.add(btnEllipse);
		buttonPanel.add(btnLine);
		buttonPanel.add(btnPen);

		chatArea.setEditable(false);
		mainPanel.add(buttonPanel, "wrap");
		mainPanel.add(drawingArea, "wrap, height 400, width 600");
		mainPanel.add(chatPanel, "wrap, height 100, width 600");
		mainPanel.add(chatInput, "width 600");
		
	}

	private void addListeners() {
		this.chatInput.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					String msg = username + ": " + chatInput.getText();
					comm.addTask(TaskFactory.createAddRoomChatMessageTask(msg));
					chatInput.setText("");
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

		});
		this.btnEllipse.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				drawingArea.setState(State.ELLIPSE);

			}
		});
		this.btnRect.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				drawingArea.setState(State.RECT);

			}
		});
		this.btnLine.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				drawingArea.setState(State.LINE);

			}
		});
		
		this.btnPen.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {

				// TODO Auto-generated method stub
				drawingArea.addFigure(new Rectangle(10,10,20,20));
				
			}
		});
		
		drawingArea.addEventListener(new MyEventClassListener() {
			
			@Override
			public void handleMyEventClassEvent(EventObject e) {
				System.out.println("FIGURE ADDED2");
				//ADDING FIGURE
				System.out.println("client GOT!! figure at: " + DateTime.now().millisOfDay().get());
				comm.addTask(TaskFactory.createAddFigureTask((Shape)e.getSource()));
				addFigure((Shape)e.getSource());
			}
		});
	}
	public void addFigure(Shape shape)
	{
		drawingArea.addFigure(shape);
	}
	public void addChatMessage(String text) {
		String actualText = this.chatArea.getText();
		actualText += text + "\n";
		chatArea.setText(actualText);
	}


	private void closeWindow() {
		mainFrame.dispose();
	}

}
